﻿using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;


namespace zymain
{
    public partial class FrmLtcs : Form
    {
        private string dztm;
        private string jszh;
        ProcessShareData obj = new ProcessShareData();
        //ProcessReadData obj = new ProcessReadData();
        //ProcessWriteData robj = new ProcessWriteData();
        public FrmLtcs()
        {
            InitializeComponent();
        }

        private void gzrBtn_Click(object sender, EventArgs e)
        {
            FrmLtgzr f = new FrmLtgzr();
            f.ShowInTaskbar = false;
            f.Owner = this;
            f.Show();
        }

        private void FrmLtcs_Load(object sender, EventArgs e)
        {
            this.czKay.SelectedIndex = 0;
        }
        private int q = 0;
        private void dztmTxt_MouseClick(object sender, MouseEventArgs e)
        {
            string dztm = this.dztmTxt.Text.Trim();
            if (string.IsNullOrEmpty(dztm))
            {
                obj.init();
                obj.WriteData2SharedMemory(q.ToString(), null);
                //Thread.Sleep(2);
                if (obj.m_str.Length > 0)
                {
                    this.dztmTxt.Text = obj.m_str;
                    KeyPressEventArgs e1 = new KeyPressEventArgs('\r');
                    dztm_KeyPress(this.dztmTxt, e1);
                }
                q++;
            }
        }


        private void dztm_TextChanged(object sender, EventArgs e)
        {
            string dztm = this.dztmTxt.Text.Trim();
            if (string.IsNullOrEmpty(dztm))
            {
                obj.init();
                obj.WriteData2SharedMemory(q.ToString(), null);
                //Thread.Sleep(2);
                if (obj.m_str.Length > 0)
                {
                    this.dztmTxt.Text = obj.m_str;
                    KeyPressEventArgs e1 = new KeyPressEventArgs('\r');
                    dztm_KeyPress(this.dztmTxt, e1);
                }
                q++;
            }
        }

        private void dztm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                //LtSettings.GetCqfk("21030223","");
                this.dztm = this.dztmTxt.Text.Trim();
                bool flag = false;
                double qk = 0.00; int ywj = 0, kwj = 0;
                string dzxm = string.Empty, szdw = string.Empty, type = string.Empty,dzjb = string.Empty;
                string sex =string.Empty;
                string strerr = string.Empty;
                this.pictureBox1.Image = null;
                if (!string.IsNullOrEmpty(dztm))
                {
                    using (DbOperHandler _open = new Common().Doh())
                    {
                        _open.Reset();
                        _open.ConditionExpress = "读者条码='" + dztm + "'";
                        if (_open.Exist("读者库"))
                        {
                            if (_open.GetField("读者库", "照片") != System.DBNull.Value)
                            {
                                Byte[] byteImageCol = (Byte[])(_open.GetField("读者库", "照片"));
                                MemoryStream stmImageCol = new MemoryStream(byteImageCol);
                                pictureBox1.Image = Image.FromStream(stmImageCol);
                            }
                            this.jszh = _open.GetField("读者库", "借书证号").ToString().Trim();
                            szdw = _open.GetField("读者库", "单位").ToString().Trim();
                            dzxm = _open.GetField("读者库", "姓名").ToString().Trim();
                            dzjb = _open.GetField("读者库", "读者级别").ToString().Trim();
                            sex = _open.GetField("读者库", "性别").ToString().Trim();
                            type = _open.GetField("读者库", "挂失注销").ToString().Trim();
                            qk += Convert.ToDouble(_open.GetField("读者库", "欠罚款").ToString().Trim());
                            qk += Convert.ToDouble(_open.GetField("读者库", "欠赔款").ToString().Trim());
                            ywj = Convert.ToInt32(_open.GetField("读者库", "已外借").ToString().Trim());
                            kwj = Convert.ToInt32(_open.GetField("读者库", "可外借").ToString().Trim());
                            flag = true;
                        }
                        else
                        {
                            _open.Reset();
                            _open.ConditionExpress = "借书证号='" + dztm + "'";
                            if (_open.Exist("读者库"))
                            {
                                if (_open.GetField("读者库", "照片") != System.DBNull.Value)
                                {
                                    Byte[] byteImageCol = (Byte[])(_open.GetField("读者库", "照片"));
                                    MemoryStream stmImageCol = new MemoryStream(byteImageCol);
                                    pictureBox1.Image = Image.FromStream(stmImageCol);
                                }
                                this.jszh = _open.GetField("读者库", "借书证号").ToString().Trim();
                                szdw = _open.GetField("读者库", "单位").ToString().Trim();
                                dzxm = _open.GetField("读者库", "姓名").ToString().Trim();
                                dzjb = _open.GetField("读者库", "读者级别").ToString().Trim();
                                sex = _open.GetField("读者库", "性别").ToString().Trim();
                                type = _open.GetField("读者库", "挂失注销").ToString().Trim();
                                qk += Convert.ToDouble(_open.GetField("读者库", "欠罚款").ToString().Trim());
                                qk += Convert.ToDouble(_open.GetField("读者库", "欠赔款").ToString().Trim());
                                ywj = Convert.ToInt32(_open.GetField("读者库", "已外借").ToString().Trim());
                                kwj = Convert.ToInt32(_open.GetField("读者库", "可外借").ToString().Trim());
                                flag = true;
                            }
                        }
                    }
                    qk = Math.Round(qk, 2);
                    this.xhTxt.Text = this.jszh;
                    this.xmTxt.Text = dzxm;
                    this.dwTxt.Text = szdw;
                    this.qkTxt.Text = qk.ToString();
                    this.kwjTxt.Text = kwj.ToString();
                    this.ywjTxt.Text = ywj.ToString();
                    this.sexTxt.Text = sex;
                    this.jbTxt.Text = dzjb;
                    if (!string.IsNullOrEmpty(type))
                    {
                        flag = false;
                        strerr = "该读者已被【" + type + "】";
                    }
                    if (qk > 0)
                    {
                        flag = false;
                        strerr = "该读者尚有欠款未缴清！欠款金额为：" + qk;
                    }
                    if (flag)
                    {
                        this.mboxTxt.Visible = false;
                        this.btmTxt.ReadOnly = false;
                    }
                    else
                    {
                        this.mboxTxt.Text = strerr;
                        this.btmTxt.ReadOnly = true;
                        this.mboxTxt.Visible = true;
                    }
                }
            }            
        }
    }
}
